package settings

import (
	"black_mamba/global"
	"black_mamba/pages"
	_ "embed"
	"gitee.com/wengo/windows_gui/widget"
	"gitee.com/wengo/windows_gui/xc"
	"gitee.com/wengo/windows_gui/xcc"
)

type ProxyPage struct {
	Foundation        pages.Foundation
	proxyUserNameEdit *widget.Edit
	proxyPasswordEdit *widget.Edit
	saveProxyBtn      *widget.Button
}

func NewProxyPage() *ProxyPage {
	proxyPage := &ProxyPage{
		Foundation: pages.Foundation{
			ExecuteBtn: nil,
			LogEdit:    nil,
			Style:      nil,
		},
		proxyUserNameEdit: widget.NewEditByName("proxyUserNameEdit"),
		proxyPasswordEdit: widget.NewEditByName("proxyPasswordEdit"),
		saveProxyBtn:      widget.NewButtonByName("saveProxyBtn"),
	}

	// 绑定执行按钮事件
	proxyPage.saveProxyBtn.Event_BnClick(proxyPage.save)

	// 初始化数据
	//proxyPage.InitProxyType()
	proxyPage.GetProxy()

	return proxyPage
}

/* 事件回调 */
func (a *ProxyPage) save(pbHandled *bool) int {
	var name, password string

	//var proxyConfig = ProxyConfig{}
	a.proxyUserNameEdit.GetText(&name, 1024)
	a.proxyPasswordEdit.GetText(&password, 1024)

	_, err := global.BM_CONFIG.Section("proxy").NewKey("username", name)
	_, err = global.BM_CONFIG.Section("proxy").NewKey("password", password)

	if err != nil {
		xc.XNotifyMsg_WindowPopup(global.BM_WINDOW.Handle, xcc.Position_Flag_Bottom, "系统提示: ", "保存失败", 0, xcc.NotifyMsg_Skin_Error)
	} else {
		global.BM_CONFIG.SaveTo("config.ini")
		xc.XNotifyMsg_WindowPopup(global.BM_WINDOW.Handle, xcc.Position_Flag_Bottom, "系统提示: ", "保存成功", 0, xcc.NotifyMsg_Skin_Success)
	}

	return 0
}

//func (a *ProxyPage) InitProxyType() {
//	a.proxyTypeCombo.CreateAdapter()
//	a.proxyTypeCombo.AddItemText("宽带连接")
//	a.proxyTypeCombo.AddItemText("隧道代理")
//}
//

func (a *ProxyPage) GetProxy() {
	username := global.BM_CONFIG.Section("proxy").Key("username").Value()
	password := global.BM_CONFIG.Section("proxy").Key("password").Value()
	a.proxyUserNameEdit.SetText(username)
	a.proxyPasswordEdit.SetText(password)
}
